package com.ex.mzwqz.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import com.ex.mzwqz.R;
import com.ex.mzwqz.adapter.AdPageAdapter;
import com.ex.mzwqz.adapter.NewsAdapter;
import com.ex.mzwqz.model.NewsEntity;
import com.ex.mzwqz.util.Constants;
import com.ex.mzwqz.util.UiManager;
import com.ex.mzwqz.widget.AutoScrollBannerView;
import com.limxing.xlistview.view.XListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaguoqiang on 16/10/19.
 */
public class NewsFragment extends BaseFragment implements XListView.IXListViewListener {

    private int infoType = Constants.SYS_INFO_TYPE_NEWS;
    private String title;
    private XListView lvNews;
    private NewsAdapter adtNews;

    public void setTitle(String title){
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public NewsFragment(int infoType) {
        this.infoType = infoType;
    }

    @Override
    protected int setContentViewResId() {
        return R.layout.fragment_news;
    }

    @Override
    protected void initView() {
        lvNews = (XListView) findViewById(R.id.lv_refresh_news);
        lvNews.setPullRefreshEnable(true);
        lvNews.setPullLoadEnable(true);
        lvNews.setXListViewListener(this);
        adtNews = new NewsAdapter(getActivity(), R.layout.item_news);
        lvNews.setAdapter(adtNews);
    }

    @Override
    protected void initData() {
//        for (int i = 0 ; i < 10 ; i ++) {
        switch (infoType){
            case 1:
                adtNews.addAll(getNew());
                break;
            case 2:
                adtNews.addAll(getPolicy());
                break;
            case 3:
                adtNews.addAll(getKnowdge());
                break;
            case 0:
                adtNews.addAll(getAll());
                AutoScrollBannerView adView = getAdView();
//                adView.setVisibility(View.VISIBLE);
//                adView.setAdapter(new AdPageAdapter(getActivity(),getAds()));
                lvNews.addHeaderView(adView,null,false);
                adView.setAdapter(new AdPageAdapter(getActivity(),getAds()));
                break;
        }

//        }
    }

    private AutoScrollBannerView getAdView(){
        AutoScrollBannerView adView = (AutoScrollBannerView) LayoutInflater.from(getContext()).inflate(R.layout.view_page_ads, lvNews,false);
        return adView;
    }

    public List<NewsEntity> getAds(){
        List<NewsEntity> list = new ArrayList<>();
//        for(int i=0; i<10; i++){
        list.add(new NewsEntity("555815","张春贤：实施“一带一路”战略 新疆大有可为","政策法规","2016-6-6","http://zs.qinqinz.com/UploadFiles/editor/image/20150606/20150606152966986698.jpg"));
        list.add(new NewsEntity("555973","习近平在宁夏考察时强调 解放思想真抓实干奋力前进 确保与全国同步建成全面小康社会","新闻","2016-7-25","http://zs.qinqinz.com/UploadFiles/editor/image/20160725/2016072510080193193.jpg"));
        list.add(new NewsEntity("555947","葡萄架下串起民族团结硕果","新闻","2016-7-4","http://zs.qinqinz.com/UploadFiles/editor/image/20160704/20160704111195009500.jpg"));
//        }

        return list;
    }

    public List<NewsEntity> getAll(){
        List<NewsEntity> list = new ArrayList<>();
//        for(int i=0; i<10; i++){
        list.add(new NewsEntity("556054","伊犁州召开现场会 推进化解钢铁行业过剩产能工作","新闻","2016-9-21",null));
        list.add(new NewsEntity("555815","张春贤：实施“一带一路”战略 新疆大有可为","政策法规","2016-6-6","http://zs.qinqinz.com/UploadFiles/editor/image/20150606/20150606152966986698.jpg"));
        list.add(new NewsEntity("555973","习近平在宁夏考察时强调 解放思想真抓实干奋力前进 确保与全国同步建成全面小康社会","新闻","2016-7-25","http://zs.qinqinz.com/UploadFiles/editor/image/20160725/2016072510080193193.jpg"));
        list.add(new NewsEntity("555857","新疆维吾尔自治区宗教事务条例","政策法规","2015-11-18",null));

//        }

        return list;
    }

    public List<NewsEntity> getPolicy(){
        List<NewsEntity> list = new ArrayList<>();
//        for(int i=0; i<10; i++){
        list.add(new NewsEntity("555857","新疆维吾尔自治区宗教事务条例","政策法规","2015-11-18",null));
        list.add(new NewsEntity("555815","张春贤：实施“一带一路”战略 新疆大有可为","政策法规","2016-6-6","http://zs.qinqinz.com/UploadFiles/editor/image/20150606/20150606152966986698.jpg"));

//        }

        return list;
    }

    public List<NewsEntity> getKnowdge(){
        List<NewsEntity> list = new ArrayList<>();
        return list;
    }

    public List<NewsEntity> getNew(){
        List<NewsEntity> list = new ArrayList<>();
//        for(int i=0; i<10; i++){
            list.add(new NewsEntity("556054","伊犁州召开现场会 推进化解钢铁行业过剩产能工作","新闻","2016-9-21",null));
            list.add(new NewsEntity("555973","习近平在宁夏考察时强调 解放思想真抓实干奋力前进 确保与全国同步建成全面小康社会","新闻","2016-7-25","http://zs.qinqinz.com/UploadFiles/editor/image/20160725/2016072510080193193.jpg"));

//        }

        return list;
    }

    @Override
    protected void initEvent() {
        lvNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                UiManager.toWebView(getActivity(),"新闻详情",Constants.NEWS_BASE_URL + adtNews.getItem(i - 1).getId());
            }
        });
    }

    @Override
    public void onRefresh() {
        lvNews.stopRefresh();
    }

    @Override
    public void onLoadMore() {
        lvNews.stopLoadMore();
    }
}
